#include "stdafx.h"

#include "context.h"
#include "renderer_exception.h"

void CContext::makeFromHWND(const HWND w)
{
	unsigned int pf;

	PIXELFORMATDESCRIPTOR pfd = {
		sizeof(PIXELFORMATDESCRIPTOR),
		1,					// version number
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		32,					// color depth
		0, 0, 0, 0, 0, 0,	// color bits
		0,					// alpha buffer
		0,					// shift bit
		0,					// accumulation buffer
		0, 0, 0, 0,			// accum bits
		32,					// zbuffer size
		0,					// stencil buffer
		0,					// auxiliary buffer
		PFD_MAIN_PLANE,		// main layer
		0,					// reserved
		0, 0, 0				// layer masks
	};
	if (!(_hdc = GetDC(w)))
		throw CRendererException("Invalid device context !");
	if (!(pf = ChoosePixelFormat(_hdc, &pfd)))
		throw CRendererException("Invalid Pixel Format !");
	if(!SetPixelFormat(_hdc, pf, &pfd))
		throw CRendererException("Invalid Pixel Format !");
	if (!(_hglrc = wglCreateContext(_hdc)))
		throw CRendererException("Couldn't create render context !");
}

void CContext::set(HDC dc, HGLRC rc)
{
		if (!(_hdc = dc))
			throw CRendererException("Couldn't get device context !");
		if (!(_hglrc = rc))
			throw CRendererException("Couldn't get render context !");
}
